/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.model.admin;

import com.common.json.GenJsonArray;
import com.admin.model.EmployeeFacadeLocal;
import com.common.json.JsonModel;
import com.entities.Employee;
import java.util.Date;
import javax.ejb.EJB;
import javax.ejb.Stateful;
import org.json.simple.JSONObject;

/**
 *
 * @author _HungChilvas_
 */
@Stateful
public class EmployeeModel implements EmployeeModelRemote {

    @EJB
    private EmployeeFacadeLocal employeeFacade;

    GenJsonArray genJson = new GenJsonArray();

    @Override
    public String listEmployee() {
        String JSONResult = genJson.genJsonArray(employeeFacade.findAll());
        return JSONResult;
    }

    @Override
    public String listSearchEmployee(Object key) {
        String JSONResult = genJson.genJsonArray(employeeFacade.find(key));
        return JSONResult;
    }

    @Override
    public boolean createEmployee(String emJson) {
        Employee em = getEmployeeObject(emJson);
        if (em.getUsername() != null) {            
            return employeeFacade.createEmployee(em);
        } else {
            return false;
        }
    }

    private Employee getEmployeeObject(String emJson) {
        JSONObject json = JsonModel.JsonDecoder(emJson);
        String Username = json.get("Username").toString();
        String Password = json.get("Password").toString();
        String Email = json.get("Email").toString();
        String Fullname = json.get("Fullname").toString();
        String Gender = json.get("Gender").toString();
        String Birthdate = json.get("Birthdate").toString();
        String Phone = json.get("Phone").toString();
        String Mobilephone = json.get("Mobilephone").toString();
        String Address = json.get("Address").toString();
        String IdentityCard = json.get("IdentityCard").toString();
        String JoinInDate = json.get("JoinInDate").toString();
        String Position = json.get("Position").toString();
        String Departments = json.get("Departments").toString();
        String Groups = json.get("Groups").toString();
        String Status = json.get("Status").toString();
        String Online = json.get("Online").toString();
        String Logged = json.get("Logged").toString();
        String Avatar = json.get("Avatar").toString();
        Date joinedTime = JsonModel.convertStringToSmallDatetime(JoinInDate);
        Date loggedTime = JsonModel.convertStringToSmallDatetime(Logged);
        Date birthtime = JsonModel.convertStringToDatetime(Birthdate);
        boolean onlineStatu = false;
        if (Online.contains("1")) {
            onlineStatu = true;
        }
        Employee em = new Employee(Username, Password, Email, Fullname, Gender, birthtime, Phone, Mobilephone, Address, IdentityCard);
        em.setPosition(Position);
        em.setDepartments(Departments);
        em.setGroups(Groups);
        em.setStatus(Status);
        em.setAvatar(Avatar);
        em.setJoinInDate(joinedTime);
        em.setLogged(loggedTime);
        em.setOnline(onlineStatu);
        return em;
    }

}
